package ckR

import (
	"database/sql"
	"db2s/dbExec"
	mq "db2s/topic-mq"
	"fmt"
	"strings"
)

func shardDirect(n string, message mq.Message) (s string) {
	//var (
	//	tableName = string(message.Body)
	//	plan      = message.Properties["plan"].(map[string]*SchedulePlan2)[tableName]
	//)
	//switch n {
	//case "src":
	//	s = plan.sshardName
	//case "dst":
	//	s = plan.dshardName
	//}
	return
}
func (sp *SchedulePlan2) routineRead(message mq.Message, n string, bb chan *int64) {
	//var (
	//	tableName = string(message.Body)
	//	plan      = message.Properties["plan"].(map[string]*SchedulePlan2)[tableName]
	//)
	//for {
	//	select {
	//	case c, ok := <-bb:
	//		if !ok {
	//			return
	//		} else {
	//			switch n {
	//			case "src":
	//				atomic.AddInt64(sps.cssum, *c)
	//				PlanContext.SubTaskBarAccumulate(map[string]any{"subTask": plan.subTaskInfo, "select": *c, "insert": int64(0)})
	//			case "dst":
	//				atomic.AddInt64(sps.cdsum, *c)
	//				PlanContext.SubTaskBarAccumulate(map[string]any{"subTask": plan.subTaskInfo, "select": int64(0), "insert": *c})
	//			}
	//		}
	//	}
	//}
}
func indexColumnDispos(message mq.Message) (indexColumn []string) {
	//	var (
	//		tableName  = string(message.Body)
	//		shardTable = message.Properties["shardTable"].(string)
	//		plan       = message.Properties["plan"].(map[string]*SchedulePlan2)[tableName]
	//	)
	//	if len(shardTable) > 0 {
	//		indexColumn = []string{"_hidden_pk_"}
	//	} else {
	//		indexColumn = plan.indexColumn
	//	}
	return
}
func idxcDispos(message mq.Message) (idxc dbExec.IndexColumnStruct) {
	//var (
	//	tableName   = string(message.Body)
	//	querySource = message.Properties["QuerySource"].(string)
	//	plan        = message.Properties["plan"].(map[string]*SchedulePlan2)[tableName]
	//)
	//if strings.EqualFold(querySource, "src") {
	//	idxc = plan.idxc
	//} else if strings.EqualFold(querySource, "dst") {
	//	idxc = plan.dstIdxc
	//}
	return
}
func dbDispos(message mq.Message) (db map[string]*sql.DB) {
	//var (
	//	querySource = message.Properties["QuerySource"].(string)
	//)
	//if strings.EqualFold(querySource, "src") {
	//	db = GlobalPConfigs.SDB
	//} else if strings.EqualFold(querySource, "dst") {
	//	db = GlobalPConfigs.DDB
	//}
	return
}
func schemaTableDispos(message mq.Message) (schema, table string) {
	var (
		tableName  = string(message.Body)
		shardTable = message.Properties["shardTable"].(string)
		St         = func(shard, table string) (s, t string) {
			if shardTable != "" {
				s = strings.Split(shard, ".")[0]
				t = strings.Split(shard, ".")[1]
			} else {
				s = strings.Split(table, ".")[0]
				t = strings.Split(table, ".")[1]
			}
			return
		}
	)
	return St(shardTable, tableName)
}
func tableMetaDispos(message mq.Message) (oo []map[string]string) {
	//var (
	//	tableName   = string(message.Body)
	//	querySource = message.Properties["QuerySource"].(string)
	//	shardName   = message.Properties["shardName"].(string)
	//	shardTable  = message.Properties["shardTable"].(string)
	//	tableMeta   map[string][]global.TableMeta
	//	plan        = message.Properties["plan"].(map[string]*SchedulePlan2)[tableName]
	//	err         error
	//)
	//if strings.EqualFold(querySource, "src") {
	//	if len(shardTable) > 0 {
	//		schema, table := schemaTableDispos(message)
	//		if tableMeta, err = GlobalISObjects.TableMetaData["source"].TableAllColumn(GlobalPConfigs.SDB[shardName], map[string]int{fmt.Sprintf("%v%v%v", schema, global.SchemaTableSplit, table): 1}); err != nil {
	//			return
	//		}
	//		for _, da1 := range tableMeta {
	//			for _, da2 := range da1 {
	//				oo = append(oo, global.StructToMap(da2))
	//			}
	//		}
	//	} else {
	//		oo = plan.idxc.TableColumn
	//	}
	//} else if strings.EqualFold(querySource, "dst") {
	//	if len(shardTable) > 0 {
	//		schema, table := schemaTableDispos(message)
	//		if tableMeta, err = GlobalISObjects.TableMetaData["dest"].TableAllColumn(GlobalPConfigs.DDB[shardName], map[string]int{fmt.Sprintf("%v%v%v", schema, global.SchemaTableSplit, table): 1}); err != nil {
	//			return
	//		}
	//		for _, da1 := range tableMeta {
	//			for _, da2 := range da1 {
	//				oo = append(oo, global.StructToMap(da2))
	//			}
	//		}
	//	} else {
	//		oo = plan.dstIdxc.TableColumn
	//	}
	//}
	return
}
func countTableQuery(message mq.Message) (D int64, err error) {
	var (
		shardName    = message.Properties["shardName"].(string)
		sqlWhere     = message.Properties["sqlwhere"].(map[string]string)
		logThreadSeq = message.Properties["logSeq"].(int64)
		event        = "[general query table sql]"
	)
	debugWriteInfo(fmt.Sprintf("(%d) %s sync data source query chunk where is %v", logThreadSeq, event, sqlWhere))
	indexColumn := indexColumnDispos(message)
	schema, table := schemaTableDispos(message)
	tableMeta := tableMetaDispos(message)
	ll := idxcDispos(message)
	ll.TableColumn = tableMeta
	ll.Schema = schema
	ll.Table = table
	ll.ColumnName = indexColumn
	ll.ShardName = shardName
	selectColumn := indexColumn[0]
	//查询该表的列名和列信息xz
	if D, err = dbExec.TQuery(ll).IndexColumnPartQuryGroupCount1(dbDispos(message), sqlWhere, selectColumn, logThreadSeq); err != nil {
		return
	}
	return
}

func partitionShardSplit(tableName string, shardinfo map[string]string) (s []map[string]string) {
	//if len(shardinfo) == 0 {
	//	return
	//}
	//shardI := shardinfo["shardInfo"]
	//queryT := shardinfo["queryT"]
	//if strings.HasPrefix(shardI, "shard") || strings.EqualFold(shardI, "single") {
	//	s = append(s, map[string]string{"queryT": queryT, "tableName": tableName, "shardName": shardI})
	//	return s
	//}
	//if n := strings.Index(shardI, global.PartitionTypeSplit); n != -1 {
	//	shardI = shardI[n+len(global.PartitionTypeSplit):]
	//}
	//shardInfo := strings.Split(shardI, global.PartitionSplit)
	//md := tm.GetTableStrconvO()
	//if len(shardInfo) > 0 {
	//	for _, v := range shardInfo {
	//		s1 := strings.Split(strings.ReplaceAll(v, global.SchemaTableSplit, "."), global.ShardNameSplit)
	//		if len(GlobalPConfigs.schema.MapTables) > 0 {
	//			s2 := strings.Split(s1[1], ".")
	//			md.Schema = s2[0]
	//			md.Table = s2[1]
	//			md.TableMap = GlobalPConfigs.schema.MapTables
	//			mschema, mtable := tm.SchemaTableCorver(*md)
	//			s = append(s, map[string]string{"queryT": queryT, "shardTable": fmt.Sprintf("%v.%v", mschema, mtable), "shardName": s1[0]})
	//		} else {
	//			s = append(s, map[string]string{"queryT": queryT, "shardTable": s1[1], "shardName": s1[0]})
	//		}
	//	}
	//}
	return s
}
